<?php
require_once('util/txt2array.php');
require_once('util/array2csv.php');
require_once('util/QuestionDO.php');
$_dbc = ['Ａ', 'Ｂ', 'Ｃ', 'Ｄ', 'Ｅ', 'Ｆ', 'Ｇ'];
$_sbc = ['A', 'B', 'C', 'D', 'E', 'F', 'G'];
$_sbc_ = ['A.', 'B.', 'C.', 'D.', 'E.', 'F.', 'G.'];

$name = "src222";
$lines = txt2Array("question/{$name}.txt");
$lines = array_values(array_filter($lines, "trim"));


$type = ""; //题型
$questionIdx = 0;
$answerStrAll = "";   //答案
$score = "0";
$courseCode = "377002";
$QA = "Q";


$questionMap = [];
//处理data
foreach ($lines as $item) {
    $item = trim($item);
    $item = str_replace("\t", "", $item);
//    题型，分数
    if (preg_match("/(单)(项)?(选)/", $item)) {
        $score = 1;
        $type = "单选题";
        continue;
    }
    if (preg_match("/(多)(项)?(选)/", $item)) {
        $score = 2;
        $type = "多选题";
        continue;
    }
    if (strpos($item, "标准答案") !== false) {
        $QA = "A";
        continue;
    }
    if ($QA === "Q") {
        $questionMap = createQuestion($item, $type, $score, $questionMap);
        formOperation($item, end($questionMap));
    } else if ($QA === "A") {
//    答案
        if (preg_match("/^[0-9]{1,3}/", $item)) {
            $answerStrAll .= $item;
        }
    }
}
formAnswer($answerStrAll, $questionMap);

$data = [];
foreach ($questionMap as $number => $question) {
    if (!empty($question->getStem())) {

        $data[] = [$question->getType(), "", $question->getStem(), $question->getOperation(), $question->getAnswer(), "", $question->getLevel(), $question->getScore(), $courseCode];
    }

}
$file_name = "util\csv\{$name}";
toCSV($file_name, $data, getHeaders());


//选择题。题干，难易
function createQuestion($item, $type, $score, $questionMap): array
{
    if (preg_match("/^[0-9]{1,3}/", $item, $questionNOArr)) {
        $questionIdx = $questionNOArr[0];
        $stem = preg_replace("/(\()(\s)*(\))/", "{block}", $item);
        $stem = preg_replace("/(（).*(）)/", "{block}", $stem);
        if (preg_match("/(\().*[易难].*(\))$/", $stem, $levelArr)) {
            $levelStr = $levelArr[0];
        }
        if (!empty($levelStr)) {
            $stem = str_replace($levelStr, "", $stem);
        }
        $stem = preg_replace("/^[0-9]{1,3}(、)?(．)?(\.)?/", "", $stem);
        $stem = str_replace("　", "", $stem);
        $stem = str_replace(" ", "", $stem);
        $stem = str_replace("{block}", "（    ）", $stem);

        $question = new QuestionDO();
        $question->setStem($stem);
        $question->setType($type);
        $question->setScore($score);

        $levelArray = ["偏易" => 2, "偏难" => 4, "易" => 1, "难" => 5];
        if (!empty($levelStr)) {
            foreach ($levelArray as $key => $val) {
                if (strpos($levelStr, "$key") != false) {
                    $question->setLevel($val);;
                    break;
                }
            }
        }
        $questionMap[$questionIdx] = $question;
    }
    return $questionMap;
}

//选择题。选项
function formOperation($item, $question): void
{
    global $_dbc,$_sbc_;
    $item = str_replace($_dbc, $_sbc_, $item);
    if (strpos($item, "A") === 0
        || strpos($item, "B") === 0
        || strpos($item, "C") === 0
        || strpos($item, "D") === 0
        || strpos($item, "E") === 0
    ) {
        if (empty($question->getOperation())) {
            $question->setOperation($item);
        } else {
            $question->setOperation($question->getOperation() . $item);
        }
        $question->setOperation(Operation::splitOptions($question->getOperation()));
    }
}

//选择题    答案
function formAnswer($answerStrAll, $questionMap): void
{
    global $_dbc,$_sbc;
    $nonStr = [" ", "．", "（", "）"];
    $answerStrAll = str_replace($nonStr, "", $answerStrAll);
    $answerStrAll = str_replace($_dbc, $_sbc, $answerStrAll);

    preg_match_all("/[0-9]{1,5}/", $answerStrAll, $numbersArr);
    preg_match_all("/[A-Ga-g]{1,5}/", $answerStrAll, $answersArr);
    $answerMap = array_combine($numbersArr[0], $answersArr[0]);
    foreach ($questionMap as $number => $question) {
        if (!empty($question->getStem())) {
            $question->setAnswer($answerMap[$number]);
        }
    }
}